use std::collections::HashMap;

pub fn converter(input: &str, tp: &str) -> String {
    let characters = include_str!("characters.json");
    let mut characters = serde_json::from_str::<HashMap<String, String>>(characters).unwrap();
    let phrases = include_str!("phrases.json");
    let mut phrases = serde_json::from_str::<HashMap<String, String>>(phrases).unwrap();
    let mut result = String::new();
    let mut input = input.chars();
    if tp == "s2t" {
        phrases = phrases.iter()
        .map(|(k, v)| (v.clone(), k.clone()))
        .collect::<HashMap<String, String>>();
        characters = characters
        .iter()
        .map(|(k, v)| (v.clone(), k.clone()))
        .collect::<HashMap<String, String>>();
    }
    'outer: while input.clone().count() != 0 {
        for phrase in &phrases {
            if input.as_str().starts_with(phrase.0.as_str()) {
                result.push_str(phrase.1.as_str());
                input.by_ref().take(phrase.0.chars().count()).for_each(drop);
                continue 'outer;
            }
        }
        // phrases not found
        let ch = input.next().unwrap().to_string();
        result.push_str(characters.get(&ch).unwrap_or(&ch));
    }
    result
}
